<?php
require_once '../../classes/includeAll_Layer2.php';
require_once '../../resources/getConnection.php';

$function  = filter_input(INPUT_POST,"function");
$message = "";
$success = "";
switch($function)
{
    case 'update':    
        $roomAndLevel = new roomAndLevel($connection);            
   
        $entries = $_POST['entry'];

        foreach ($entries as $key => $entry) 
        {
             $roomAndLevel->ID=$key;
             $roomAndLevel->getInfo();
             
             if(isset($entry[0]))
             {
                 if($entry[0] !="")
                 {
                     $roomAndLevel->SwitchPortName = $entry[0];      
                 }           
             }
             if(isset($entry[1]))
             {
                 if($entry[1] !="")
                 {
                     $roomAndLevel->SwitchName = $entry[1];
                 }           
             }
             if(isset($entry[2]))
             {
                 if($entry[2] !="")
                 {
                     $roomAndLevel->UpdatePatchPannel($entry[2], "name");
                     $roomAndLevel->PatchPanelName = $entry[2];
                 }           
             }
             if(isset($entry[3]))
             {
                 if($entry[3] !="")
                 {
                     $roomAndLevel->RoomName = $entry[3];
                 }           
             }
             if(isset($entry[4]))
             {
                 if($entry[4] !="")
                 {
                     $roomAndLevel->RoomNumber = $entry[4];
                 }           
             }
             if(isset($entry[5]))
             {
                 if($entry[5] !="")
                 {
                     $roomAndLevel->LevelName = $entry[5];
                 }           
             }
             $roomAndLevel->AllExistsOrCreate();       
        }  
        $success = "true";
        $message = "true";
                    
    break;

    case 'add':
        $switchPort  = filter_input(INPUT_POST,"switchPortNew");
        $switch  = filter_input(INPUT_POST,"switchNew");
        $patchName  = filter_input(INPUT_POST,"patchNameNew");
        $roomName  = filter_input(INPUT_POST,"roomNameNew");
        $roomNumber  = filter_input(INPUT_POST,"roomNumberNew");
        $levelName  = filter_input(INPUT_POST,"levleNameNew");
               
        
        if($switchPort !='' && $switch !='' && $patchName !='' && $roomName !='' && $roomNumber !='')
        {
         $roomAndLevel = new roomAndLevel($connection); 
         
         $roomAndLevel->SwitchPortName = $switchPort;
         $roomAndLevel->SwitchName = $switch;
         $roomAndLevel->PatchPanelName = $patchName;
         $roomAndLevel->RoomName = $roomName;
         $roomAndLevel->RoomNumber = $roomNumber;
         $roomAndLevel->LevelName = $levelName;
         
         $roomAndLevel->AllExistsOrCreate();
       
         $message = array($switchPort, $switch, $patchName, $roomName, $roomNumber, $levelName, $roomAndLevel->ID);
         
          $success = "true";
        }else
        {
          $success = "false";
        }
    break;
      
    case 'delete':
        $roomAndLevel = new roomAndLevel($connection);  
        $ID = $_POST["patchpanelID"];
        $message = "ID: " . $ID;
        if(isset($ID))
        {
            $roomAndLevel->ID = $ID;
            $roomAndLevel->getInfo();
            $roomAndLevel->DeletePatchPanel();
            $roomAndLevel->DeleteSwitchPort();
            if($roomAndLevel->DeleteSwitchPort())
            {
                $message = "true";
            }else
            {
                $roomAndLevel->PatchpannelEntryExistsOrCreate();
                $message = "false";
            }
            $success = "true";
            //$message = 'test';
        }
        
    break;
    
    case 'deleteChecked':
        $successArr = array();
        if(isset( $_POST['del']))
        { 
            $roomAndLevel = new roomAndLevel($connection); 
            $deleteArray = $_POST['del'];
                    foreach($deleteArray  as $key => $value) 
                    {
                        $roomAndLevel->ID = $key;
                        $device = new Device($key, $connection);
                        $roomAndLevel->getInfo();
                        $roomAndLevel->DeletePatchPanel();
                        $roomAndLevel->DeleteSwitchPort();
                        if($roomAndLevel->DeleteSwitchPort())
                        {
                            array_push($successArr, $key);
                            $message = "true";
                            $success = "true";
                        }else
                        {
                            $roomAndLevel->PatchpannelEntryExistsOrCreate();
                        }
                    }    
                    $success = "true"; 
                    $message = $successArr;
        }else
        {
            $success = "false"; 
            $message = "Kein Gerät ausgewählt";
        }
    break;
    default:
        $success = "false";
        $message ="Ungültige Anweisung";
    break;
}
echo json_encode (array("success"=>$success, "message"=>$message));

$connection->disconnectDB();
?>


